/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.baseDataView;
/**
 * 说明：
 * 添加部门界面
 * @Author：黄盛强
 * @Date：2012-03-20
 */
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ruipengkj.bean.TExePosCatalog;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelJTextAreaComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.RPJButtonWhite;
import com.ruipengkj.component.jdialog.BorderJPanel;
import com.ruipengkj.component.jdialog.IDialog;
import com.ruipengkj.service.TExePosCatalogService;

public class DataExePosCatalogAddDialog extends IDialog implements ActionListener {
	

	private static final long serialVersionUID = 1L;
	
	private JLabelJTextFieldComponent deptID; //部门ID
	private JLabelJTextFieldComponent name; //部门名字
	private JLabelJTextAreaComponent des; //说明
	
	private TExePosCatalogService TExePosCatalogService; 
	private JButton yesButton; //确定按钮
	private JButton noButton; //取消按钮
	
	public boolean isSuccess=false;
	private TExePosCatalog TExePosCatalog=null;
	public DataExePosCatalogAddDialog(JDialog jd, String title, boolean b) {
		super(jd, title, b);
		TExePosCatalogService=BuildSpringApplication.getInstance().getTExePosCatalogServiceImpl();
		this.init();
		this.initListener();
		this.setSize(350, 300);
		this.setLocationRelativeTo(null);
	}
	
	private void init() {
		JPanel main_panel = new BorderJPanel();
		BoxLayout boxLayout = new BoxLayout(main_panel, BoxLayout.Y_AXIS);
		main_panel.setBorder(BorderFactory.createTitledBorder("场所分类添加"));
		main_panel.setLayout(boxLayout);
		main_panel.setBackground(Util.PANEL_COLOR);
		
		deptID = new JLabelJTextFieldComponent("*", "场所分类编号：", 30);
		deptID.getText().setEditable(false);
		deptID.getText().setText(UUID.GenUUID15());
		name = new JLabelJTextFieldComponent("*", "场所分类名称：", 30);
		des = new JLabelJTextAreaComponent("  场所分类说明：", 23,2);
		
		yesButton = new RPJButtonWhite("保存(Ctrl+S)");
		noButton = new RPJButtonWhite("取消(Ctrl+Q)");
		
		main_panel.add(deptID);
		main_panel.add(name);
		main_panel.add(des);
		JPanel jp4 = new JPanel(new FlowLayout(FlowLayout.CENTER));
		jp4.add(yesButton);
		jp4.add(noButton);
		jp4.setBackground(Util.PANEL_COLOR);
		main_panel.add(jp4);
		main_panel.setCursor(Util.CURSOR_DEFAULT);
		
		this.add(main_panel);
	}

	private void initListener(){
		yesButton.addActionListener(this);
		yesButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		noButton.addActionListener(this);
		noButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_Q, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	
	@Override
	protected Dimension getTitleSize() {
		return new Dimension(120, 29);
	}
	
	// 修改查看
	public void showDialog(String deptID) {
		if (deptID != null && !deptID.equals("")) {
			TExePosCatalog TExePosCatalog = null;
			try {
				TExePosCatalog = TExePosCatalogService.getByIdService(deptID);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			this.deptID.getText().setText(TExePosCatalog.getPosCatalogID());
			this.name.getText().setText(TExePosCatalog.getName());
			this.des.getArea().setText(TExePosCatalog.getDes());
		}
		this.setVisible(true);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == yesButton) {
			String str_deptID = this.deptID.getText().getText().trim();
			String str_name = this.name.getText().getText().trim();
			String str_des = this.des.getArea().getText().trim();
			
			if(str_name.equals("")||str_name.length()>64){
				JOptionPane.showMessageDialog(null, "场所分类名称不能为空，且不能大于64个字");
				name.requestFocus();
				return;
			}
			
			if(str_des.length()>512){
				JOptionPane.showMessageDialog(null, "分类描述不能大于512个字");
				des.requestFocus();
				return;
			}
			if(TExePosCatalog==null){
				TExePosCatalog = new TExePosCatalog();
			}
			TExePosCatalog.setPosCatalogID(str_deptID);
			TExePosCatalog.setName(str_name);
			TExePosCatalog.setDes(str_des);
			try {
				//TExePosCatalog.setId((Integer));
				TExePosCatalogService.saveOrUpdateService(TExePosCatalog);
				this.isSuccess=true;
				BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("添加实施场所分类信息");
				JOptionPane.showMessageDialog(null, "操作成功");
				this.dispose();
			} catch (Exception ee) {
				ee.printStackTrace();
				this.isSuccess=false;
				JOptionPane.showMessageDialog(null, "操作失败");
			}
		} else if(e.getSource() == noButton) {
			this.isSuccess=false;
			this.dispose();
		}
		super.actionPerformed(e);
	}

	public TExePosCatalog getTExePosCatalog() {
		return TExePosCatalog;
	}
	
	public static void main(String[] args) {
		DataExePosCatalogAddDialog dialog = new DataExePosCatalogAddDialog(null, "添加部门", true);
		dialog.setVisible(true);
	}
}
